import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf
import numpy as np

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_satscanresult_dbf(inputDBF):
    sKey = np.array([])
    fn = inputDBF
    db = dbf.Dbf(fn)
    for record in db:
        temp = float(record[db.fieldNames[2]])
        #if temp < 0.05:
        temp_id = int(float(record[db.fieldNames[0]]))
        sKey = np.append(sKey, [temp_id, temp])
    sKey.shape = (-1, 2)
    return sKey

#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    pvalueCSV = 'C:/_DATA/CancerData/test/Jun08/satscan/highlow/hl116.gis.dbf'
    pvalue = build_satscanresult_dbf(pvalueCSV)
    pvalueHighCSV = 'C:/_DATA/CancerData/test/Jun08/satscan/high/116.gis.dbf'
    phighvalue = build_satscanresult_dbf(pvalueHighCSV)
    
    pid = []
    for item in pvalue:
        pid.append(int(item[0]))
    
    phighid = []
    for item in phighvalue:
        phighid.append(int(item[0]))
    
    '''
    for item in pvalue:
        if (int(item[0]) in phighid):
            item[1] = 1
    
    
    for i in range(0,1000):
        if (i not in pid):
            pvalue = np.append(pvalue, [i, 1])
    '''
    for i in range(0,1000):
        if (i not in phighid):
            phighvalue = np.append(phighvalue, [i, 1])
            
    phighvalue.shape = (-1, 2)
    print phighvalue
    filePath = 'c:/116_satscan_high.csv'
    np.savetxt(filePath, phighvalue, delimiter=',', fmt = '%10.5f')
        